$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/caseManage/list',
        datatype: "json",
        colModel: [
            { label: '图片',name: 'picUrl',sortable:false, width: 80, formatter: function(value, options, row){

                return    value==undefined || value==null || value=="" ? '<image src="/statics/img/noImage.png" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>' : '<image src="'+value+'" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>';
            }},
            { label: '用户ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true},
            { label: '标题', name: 'caseName', width: 75,sortable:false },
            { label: '性别', name: 'sickSex', width: 35,sortable:false,formatter: function(value, options, row){

                return value==0 ? "男" : "女";

            } },
            { label: '年龄', name: 'sickAge', width: 55,sortable:false},
            { label: '基本病况', name: 'sickBaseStatus', width: 75,sortable:false },
            { label: '类型', name: 'directionType', width: 80,sortable:false, formatter: function(value, options, row){
                switch(value)
                {
                    case "1":return "关节修复与重建";
                    case "2":return "关节翻修";
                    case "3":return "下肢畸形矫正";
                    case "4":return "骨与关节感染";
                    case "5":return "风湿关节病";
                    default: return "";
                }
            }},
            { label: '更新时间', name: 'updateTime', width: 80,sortable:false}
        ],
        viewrecords: true,

        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});


/*function uploadImage(id,name)
{

    $(id).fileupload({
        url: baseURL + "fileUpload/images",
        type: "POST",
        autoUpload: true,
       /!* formData:{"token":localStorage.getItem("token")},*!/
        sequentialUploads: true,
        singleFileUploads: false,
    }).bind('fileuploadprogress', function (e, data) {

        var tempArray=[];
        for(var i=0;i<data.originalFiles.length;i++)
        {
            tempArray.push(data.originalFiles[i].name);
        }
        if(name=="caseDetail1PicUrls")
        {
            vm.caseDetail1PicUrls=tempArray.join(",");
        }else if(name=="caseDetail2PicUrls")
        {
            vm.caseDetail2PicUrls=tempArray.join(",");
        }else if(name=="caseDetail3PicUrls")
        {
            vm.caseDetail3PicUrls=tempArray.join(",");
        }else if(name=="picUrl")
        {
            vm.picUrl=tempArray.join(",");
        }



    }).bind('fileuploaddone', function (e, data) {

        if(data.result.errcode==0){
            if(name=="caseDetail1PicUrls")
                vm.case1.caseDetail1PicUrls = data.result.data.join(",");
            else if(name=="caseDetail2PicUrls")
                vm.case1.caseDetail2PicUrls = data.result.data.join(",");
            else if(name=="caseDetail3PicUrls")
                vm.case1.caseDetail3PicUrls = data.result.data.join(",");
        else if(name=="picUrl")
                vm.case1.picUrl = data.result.data.join(",");
            alert("上传成功！");
        }else{
            alert("上传失败！");
        }
    });
}*/

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            title: null
        },
        requestUrl:baseURL+"fileUpload/image",
        showList: true,
        title:null,
        case1:{
            status:0,
            type:7,
            directionType:'1',
            sickSex:0
        },
        caseDetail1PicUrls:[],//术前诊断图片
        caseDetail2PicUrls:[],
        picUrl:"",
        token:{token:localStorage.getItem("token")},
        caseDetail3PicUrls: [],
        items:[{text:'关节修复与重建',value:'1'},{text:'关节翻修',value:'2'},{text:'下肢畸形矫正',value:'3'},{text:'骨与关节感染',value:'4'},{text:'风湿关节病',value:'5'}],

    },
    methods: {
        handleRemove0(file, fileList) {
            vm.picUrl="";
        },
        handlePreview0(file) {

        },
        handleAvatarSuccess0(res, file) {
            vm.picUrl=res.data;
        },
        handleRemove1(file, fileList) {
            vm.caseDetail1PicUrls= vm.deleteImage(file,vm.caseDetail1PicUrls);
        },
        handlePreview1(file) {
        },
        handleAvatarSuccess1(res, file) {
            vm.caseDetail1PicUrls= vm.addImage(res,file,vm.caseDetail1PicUrls);
        },
        handleRemove2(file, fileList) {
            vm.caseDetail2PicUrls=vm.deleteImage(file,vm.caseDetail2PicUrls);
        },
        handlePreview2(file) {
        },
        handleAvatarSuccess2(res, file) {
            vm.caseDetail2PicUrls=vm.addImage(res,file,vm.caseDetail2PicUrls);
        },
        handleRemove3(file, fileList) {
            vm.caseDetail3PicUrls=vm.deleteImage(file,vm.caseDetail3PicUrls);
        },
        handlePreview3(file) {
        },
        handleAvatarSuccess3(res, file) {
            vm.caseDetail3PicUrls=vm.addImage(res,file,vm.caseDetail3PicUrls);
        },
        query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.picUrl="";
            vm.caseDetail1PicUrls=[];
            vm.caseDetail2PicUrls=[];
            vm.caseDetail3PicUrls=[];
            vm.case1 = {status:0,type:7,directionType:'1',sickSex:0};

        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.caseDetail1PicUrls=[];
            vm.caseDetail2PicUrls=[];
            vm.caseDetail3PicUrls=[];
            vm.picUrl="";
            vm.getUser(userId);

        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "orth/caseManage/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {

            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            /*if(vm.validator()){
                return ;
            }*/
            vm.case1.picUrl=vm.picUrl;

            vm.case1.caseDetail1PicUrls=vm.joinPicName(vm.case1.caseDetail1PicUrls,vm.caseDetail1PicUrls);
            vm.case1.caseDetail2PicUrls=vm.joinPicName(vm.case1.caseDetail2PicUrls,vm.caseDetail2PicUrls);
            vm.case1.caseDetail3PicUrls=vm.joinPicName(vm.case1.caseDetail3PicUrls,vm.caseDetail3PicUrls);

            var url = vm.case1.id_ == null ? "orth/caseManage/save" : "orth/caseManage/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.case1),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(id){
            $.get(baseURL + "orth/caseManage/info/"+id, function(r){
                vm.case1 = r.case;
                vm.picUrl=vm.case1.picUrl;

                vm.caseDetail1PicUrls=vm.updateInitPics(vm.case1.caseDetail1PicUrls,vm.caseDetail1PicUrls);
                vm.caseDetail2PicUrls=vm.updateInitPics(vm.case1.caseDetail2PicUrls,vm.caseDetail2PicUrls);
                vm.caseDetail3PicUrls=vm.updateInitPics(vm.case1.caseDetail3PicUrls,vm.caseDetail3PicUrls);



            });
        },
        reload: function () {
            vm.showList = true;
            $(".formError").hide();
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'digest': vm.q.title},
                page:page
            }).trigger("reloadGrid");
        },
        deleteImage:function (file,obj) {
            for(var i=0;i<obj.length;i++)
            {
                if(obj[i].name==file.name)
                {
                    obj.splice(i,1);
                }
            }
            return obj;
        },
        addImage:function (res, file,obj) {
            var image={name:file.name,url:res.data};
            obj.push(image);
            return obj;
        },
        joinPicName:function (caseObj,obj) {
            caseObj=[];
            for(var i=0;i<obj.length;i++)
            {
                caseObj+=obj[i].url;
                if(i!=(obj.length-1))
                    caseObj+=",";
            }
            return caseObj;
        },
        updateInitPics:function (caseObj,obj) {

            if(caseObj!=undefined && caseObj!=null && caseObj!="")
            {
                var tempArray=caseObj.split(",");
                for(var i=0;i<tempArray.length;i++)
                {
                    var picname=tempArray[i].substr(65);
                    var image={name:picname,url:tempArray[i]};
                    obj.push(image);
                }
                return obj;
            }

            return [];

        },
        validator: function () {

        }
    }
});